<?php

namespace app\common\model;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Model;
use think\Config;
class Article extends Model
{
    // 表名
    protected $name = 'article';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function getList2($where=[],$uid=null,$order="createtime desc,weight desc")
    {
        $info = Article::alias('a')
            ->field('a.*,u.avatar,u.nickname')
            ->join('User u','a.user_id = u.id','left')
            ->where($where)
            ->order($order)
            ->select();
        if($info){
            foreach ($info as &$v){
                $v['is_likes']=0;
                if($uid){
                    $vote_info = db('Vote')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                    if($vote_info){
                        $v['is_likes']=1;
                    }
                }
                if($v['images']){
                    $images = explode(',',$v['images']);
                    foreach ($images as &$vvv){
                        $vvv=request()->domain().$vvv;
                    }
                    $v['images'] = $images;
                }

                if($v['cover']){
                   $v['cover']=cdnurl($v['cover'],true);
                }

                $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
            }
        }
        return $info;
    }
    public function getList($where=[],$page=true,$follow,$uid=null,$order="createtime desc,weight desc")
    {
//        if(!$follow){
//            return [];
//        }
        //分页
        if($follow){
            $limit = 2;
//            $info = Article::alias('a')
//                ->field('a.*,u.avatar,u.nickname')
//                ->join('User u','a.user_id = u.id','left')
//                ->join('Vote v','v.aid = a.id','left')
//                ->join('Collection c','c.aid = a.id','left')
//                ->with(['child' =>function ($query) use ($limit) {
//                    $query->alias('cl')->join('user cc','cl.user_id = cc.id','left')->where('cl.level',1)->field('cl.id,cl.top_id,cc.nickname,cl.level,cc.avatar,cl.id,cl.aid,cl.user_id,cl.to_user_id,cl.createtime,cl.content')->order('cl.createtime desc')->limit(0, $limit);
//                }])
//                ->where($where)
//                ->order($order)
//                ->paginate(10);
            $info = Article::alias('a')
                ->field('a.*,u.avatar,u.nickname')
                ->join('User u','a.user_id = u.id','left')
                ->where($where)
                ->order($order)
                ->paginate(10);
            $info=json_decode(json_encode($info),true);
            if($info['data']){
                foreach ($info['data'] as &$v){
                    $v['child'] = [];
                    $child =db('comment')
                       ->alias('cl')
                       ->field('cl.id,cl.top_id,cc.nickname,cl.level,cc.avatar,cl.id,cl.aid,cl.user_id,cl.to_user_id,cl.createtime,cl.content')
                       ->join('user cc','cl.user_id=cc.id')
                       ->where(['cl.aid'=>$v['id'],'cl.level'=>1])
                       ->order('cl.createtime desc')
                       ->limit($limit)
                       ->select();
                   if($child){
                       $v['child']=$child;
                   }
                }
            }
            if($info['data']){
                foreach ($info['data'] as &$v){
                    $v['is_likes']=0;
                    $v['is_collects']=0;
                    if($uid){
                        $vote_info = db('Vote')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                        if($vote_info){
                            $v['is_likes']=1;
                        }
                        $collection_info = db('Collection')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                        if($collection_info){
                            $v['is_collects']=1;
                        }
                    }

                    $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
                    if($v['cover']){
                        $v['cover']=cdnurl($v['cover'],true);
                    }
                    if($v['images']){
                        $images = explode(',',$v['images']);
                        foreach ($images as &$vvv){
                            $vvv=request()->domain().$vvv;
                        }
                        $v['images'] = $images;
                    }
                    if($v['video']){
                        $v['video'] = request()->domain().$v['video'];
                    }
//                    if($v['child']){
//                        foreach($v['child'] as &$child_val){
//                            $child_val['createtime']=wordTime($child_val['createtime']);
//                        }
//                    }
                }
            }
        }else{
            if($page){
                $info = Article::alias('a')
                    ->field('a.*,u.avatar,u.nickname')
                    ->join('User u','a.user_id = u.id','left')
//                    ->join('Vote v','v.aid = a.id','left')
//                    ->join('Collection c','c.aid = a.id','left')
                    ->where($where)
                    ->order($order)
                    ->paginate(10);
                $info=json_decode(json_encode($info),true);
                if($info['data']){
                    foreach ($info['data'] as &$v){
                        $v['is_likes']=0;
                        $v['is_collects']=0;
                        if($uid){
                            $vote_info = db('Vote')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                            if($vote_info){
                                $v['is_likes']=1;
                            }
                            $collection_info = db('Collection')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                            if($collection_info){
                                $v['is_collects']=1;
                            }
                        }
                        if($v['cover']){
                            $v['cover']=cdnurl($v['cover'],true);
                        }
                        $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
                        if($v['images']){
                            $images = explode(',',$v['images']);
                            foreach ($images as &$vvv){
                                $vvv=request()->domain().$vvv;
                            }
                            $v['images'] = $images;
                        }
                        if(!$v['user_id'] && $v['type'] == 1){
                            $v['nickname'] = Config::get('site.nickname');
                            $v['avatar'] = cdnurl(Config::get('site.avatar'),true);
                        }
                        if($v['video']){
                            $v['video'] = request()->domain().$v['video'];
                        }
                    }
                }
            }else{
                $info = Article::alias('a')
                    ->field('a.*,u.avatar,u.nickname')
                    ->join('User u','a.user_id = u.id','left')
//                    ->join('Vote v','v.aid = a.id','left')
//                    ->join('Collection c','c.aid = a.id','left')
                    ->where($where)
                    ->order($order)
                    ->limit(6)
                    ->select();
                $info=json_decode(json_encode($info),true);
                if($info){
                    foreach ($info as &$v){
                        $v['is_likes']=0;
                        $v['is_collects']=0;
                        if($uid){
                            $vote_info = db('Vote')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                            if($vote_info){
                                $v['is_likes']=1;
                            }
                            $collection_info = db('Collection')->where(['uid'=>$uid,'aid'=>$v['id']])->find();
                            if($collection_info){
                                $v['is_collects']=1;
                            }
                        }
                        if(!$v['user_id'] && $v['type'] == 1){
                            $v['nickname'] = Config::get('site.nickname');
                            $v['avatar'] = cdnurl(Config::get('site.avatar'),true);
                        }
                        if($v['cover']){
                            $v['cover']=cdnurl($v['cover'],true);
                        }
                        $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
                        if($v['images']){
                            $images = explode(',',$v['images']);
                            foreach ($images as &$vvv){
                                $vvv=request()->domain().$vvv;
                            }
                            $v['images'] = $images;
                        }
                        if($v['video']){
                            $v['video'] = request()->domain().$v['video'];
                        }
                    }
                }
            }
        }
        return $info;
    }
    public function getTypeList($type=1,$uid,$order="a.createtime desc,weight desc")
    {
        if(!$uid){return $info=[];}
        //个人发表文章
        if($type == 1){
            $info = Article::alias('a')
                ->field('a.*,u.avatar,u.nickname,IFNULL(v.id,0) is_status')
                ->join('User u','a.user_id = u.id','left')
                ->join('vote v','v.aid = a.id and v.uid ='.$uid,'left')
                ->where('a.user_id',$uid)
                ->order($order)
                ->paginate(10);
        }elseif($type==2){
            //收藏
            $info = Collection::alias('c')
                ->field('a.*,u.avatar,u.nickname,IFNULL(v.id,0) is_status')
                ->join('Article a','c.aid = a.id','left')
                ->join('User u','a.user_id = u.id','left')
                ->join('vote v','v.aid = a.id and v.uid ='.$uid,'left')
                ->where('c.uid',$uid)
                ->order($order)
                ->paginate(10);

        }else{
            //点赞
            $info = Vote::alias('v')
                ->field('a.*,u.avatar,u.nickname,IFNULL(v.id,0) is_status')
                ->join('Article a','v.aid = a.id','left')
                ->join('User u','a.user_id = u.id','left')
                ->where('v.uid',$uid)
                ->order($order)
                ->paginate(10);
        }
        $info=json_decode(json_encode($info),true);
        if($info['data']){
            foreach ($info['data'] as $key=>&$v){
                if(!$v['id']){
                    unset($info['data'][$key]);continue;
                }
                if(!$v['user_id'] && $v['type'] == 1){
                    $v['nickname'] = Config::get('site.nickname');
                    $v['avatar'] = cdnurl(Config::get('site.avatar'),true);
                }
                if($v['images']){
                    $images = explode(',',$v['images']);
                    foreach ($images as &$vvv){
                        $vvv=request()->domain().$vvv;
                    }
                    $v['images'] = $images;
                }
                if($v['cover']){
                    $v['cover']=cdnurl($v['cover'],true);
                }
                if($v['video']){
                    $v['video'] = request()->domain().$v['video'];
                }
            }
        }
        return $info;
    }

    public function child()
    {
        return $this->hasMany('comment', 'aid', 'id');
    }
}